#include "connect_dma.h"
#include "string.h"
#include <stdarg.h>
#include <stdio.h>

uint8_t uart_rx_buffer[128];
volatile uint16_t uart_rx_len = 0;
volatile uint8_t uart_rx_flag = 0;
volatile uint8_t tx_busy = 0;

void UART_DMA_Init(void)
{
    HAL_UART_Receive_DMA(&huart1, uart_rx_buffer, sizeof(uart_rx_buffer));
    __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart->Instance == USART1)
        tx_busy = 0;
}

void UART_Send_DMA(const char *format, ...)
{
	if (1) return; 
	
    static char tx_buf[128]; 
    va_list args;
    va_start(args, format);
    vsnprintf(tx_buf, sizeof(tx_buf), format, args);
    va_end(args);

	//tx_busy = 1;
    HAL_UART_Transmit_DMA(&huart1, (uint8_t *)tx_buf, strlen(tx_buf));
}

void HAL_UART_IDLE_Callback(UART_HandleTypeDef *huart)
{
    if (huart == &huart1)
    {
        __HAL_UART_CLEAR_IDLEFLAG(&huart1);
        //HAL_UART_DMAStop(&huart1);
        uart_rx_len = sizeof(uart_rx_buffer) - __HAL_DMA_GET_COUNTER(huart1.hdmarx);
        uart_rx_flag = 1;
        HAL_UART_Receive_DMA(&huart1, uart_rx_buffer, sizeof(uart_rx_buffer));
    }
}

void UART_ProcessReceivedData(uint8_t *data, uint16_t len)
{


}
